<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<script type="text/javascript">
	$(function() {

		$('#admin_customer_datagrid').datagrid({
			url : '${pageContext.request.contextPath}/customerController/datagrid.action',
			fit : true,
			fitColumns : true,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 20,
			pageList : [ 10,20,30,40,50,100,200,300,500 ],
			sortName : 'xm',
			sortOrder : 'asc',
			checkOnSelect : false,
			selectOnCheck : false,
			nowrap : false,
			frozenColumns : [ [ {
				field : 'id',
				title : '编号',
				checkbox : true
			}, {
				field : 'xm',
				title : '姓名',
				sortable : true,
				width : 80,
			} ] ],
			columns : [ [ {
				field : 'sjh',
				title : '手机号码',
				sortable : true,
				width : 60
			}, {
				field : 'email',
				title : '电子邮箱',
				sortable : true,
				width : 60
			}, {
				field : 'ssdz',
				title : '所在单位',
				sortable : true,
				width : 60
			},{
				field : 'xkzy',
				title : '学科专业',
				sortable : true,
				width : 60
			},{
				field : 'password',
				title : '密码',
				width : 60
			},{
				field : 'tjr',
				title : '推荐人',
				sortable : true,
				width : 60
			},{
				field : 'action',
				title : '操作',
				width : 100,
				formatter : function(value, row, index) {
						return formatString('<img onclick="admin_customer_editFun(\'{0}\');" src="{1}"/><strong style="cursor:pointer" onclick="admin_customer_editFun(\'{2}\');">修改</strong>&nbsp;&nbsp;<img onclick="admin_customer_deleteFun(\'{3}\');" src="{4}"/><strong style="cursor:pointer" onclick="admin_customer_deleteFun(\'{5}\');">邀请成为荣誉会员</strong>', row.id, '${pageContext.request.contextPath}/style/images/extjs_icons/pencil.png', row.id, row.id, '${pageContext.request.contextPath}/style/images/extjs_icons/tab_add.png', row.id);
				}
			} ] ],
			toolbar : [ {
				text : '添加',
				iconCls : 'icon-add',
				handler : function() {
					admin_customer_appendFun();
				}
			}, '-', {
				text : '批量邀请成为荣誉会员',
				iconCls : 'icon-add',
				handler : function() {
					admin_customer_removeFun();
				}
			}, '-']
		});

	});
	
	function admin_customer_editFun(id) {
		$('#admin_customer_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/admin/customerEdit.jsp',
			width : 520,
			height : 300,
			modal : true,
			title : '修改志愿者信息',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-edit',
				handler : function() {
					var d = $(this).closest('.window-body');
					$('#admin_customerEdit_editForm').form('submit', {
						url : '${pageContext.request.contextPath}/customerController/edit.action',
						success : function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									$('#admin_customer_datagrid').datagrid('updateRow', {
										index : $('#admin_customer_datagrid').datagrid('getRowIndex', id),
										row : r.obj
									});
									d.dialog('destroy');
								}
								$.messager.show({
									title : '提示',
									msg : r.msg
								});
							} catch (e) {
								$.messager.alert('提示', result);
							}
						}
					});
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			},
			onLoad : function() {
				var index = $('#admin_customer_datagrid').datagrid('getRowIndex', id);
				var rows = $('#admin_customer_datagrid').datagrid('getRows');
				var o = rows[index];
				$('#admin_customerEdit_editForm').form('load', o);
			}
		});
	}
	
	function admin_customer_appendFun() {
		$('#admin_customer_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/admin/customerAdd.jsp',
			width : 520,
			height : 300,
			modal : true,
			title : '添加志愿者',
			buttons : [ {
				text : '添加',
				iconCls : 'icon-add',
				handler : function() {
					var d = $(this).closest('.window-body');
					$('#admin_customerAdd_addForm').form('submit', {
						url : '${pageContext.request.contextPath}/customerController/add.action',
						success : function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									$('#admin_customer_datagrid').datagrid('insertRow', {
										index : 0,
										row : r.obj
									});
									d.dialog('destroy');
								}
								$.messager.show({
									title : '提示',
									msg : r.msg
								});
							} catch (e) {
								$.messager.alert('提示', result);
							}
						}
					});
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			}
		});
	}
	
	function admin_customer_removeFun() {
		var rows = $('#admin_customer_datagrid').datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			$.messager.confirm('确认', '您是否要邀请选中的志愿者成为荣誉会员（注意：已邀请过的志愿者将不会被重复邀请）？', function(r) {
				if (r) {
					for (var i = 0; i < rows.length; i++) {
						ids.push(rows[i].id);
					}
					$.ajax({
						url : '${pageContext.request.contextPath}/customerController/remove.action',
						data : {
							ids : ids.join(',')
						},
						dataType : 'json',
						success : function(result) {
							if (result.success) {
								$('#admin_customer_datagrid').datagrid('load');
								$('#admin_customer_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
								if (!$('#layout_center_tabs').tabs('exists', '荣誉会员管理')) {
									$('#layout_center_tabs').tabs('add', {title : '荣誉会员管理', closable : true, iconCls : '', href : '${pageContext.request.contextPath}/admin/product.jsp'});
								} else {
									$('#layout_center_tabs').tabs('getTab', '荣誉会员管理').panel('refresh');
									$('#layout_center_tabs').tabs('select', '荣誉会员管理');
								}
							}
							$.messager.show({
								title : '提示',
								msg : result.msg
							});
						}
					});
				}
			});
		} else {
			$.messager.show({
				title : '提示',
				msg : '请勾选需要邀请的志愿者！'
			});
		}
	}
	function admin_customer_deleteFun(id) {
		$('#admin_customer_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		$('#admin_customer_datagrid').datagrid('checkRow', $('#admin_customer_datagrid').datagrid('getRowIndex', id));
		admin_customer_removeFun();
	}
	
	
</script>
<div class="easyui-layout" data-options="fit:true, border:false">
	<div data-options="region:'center',border:false">
		<table id="admin_customer_datagrid"></table>
	</div>
</div>